<template>
  <div class="login_container">
    <img class="bgimg" src="../assets/loginbg.png" alt="" />
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 欢迎登陆 -->
      <h2>欢迎登录</h2>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model.trim="loginForm.username"
            prefix-icon="iconfont icon-user"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model.trim="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="vcode">
          <el-input
            v-model.trim="loginForm.vcode"
            placeholder="请输入验证码"
            style="width: 250px"
          ></el-input>
          <img
            src="/api/users/captcha"
            alt=""
            onclick="this.src = this.src +
          '?'"
            class="vcodeImg"
            style="float: right"
          />
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登录表单的数据绑定对象
      loginForm: {
        username: "admin",
        password: "123456",
        vcode: "",
      },
      // 表单的验证规则对象
      loginFormRules: {
        //验证用户名是否合法
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
        // 验证码
        vcode: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      },
    };
  },
  methods: {
    // 重置登录表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    async login() {
      // 验证验证码
      const { data: data } = await this.$http.get(
        "/api/users/captcha/" + this.loginForm.vcode
      );
      if (data.code !== 1) return this.$message.error(data.msg);
      // 验证账号密码
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("/api/users/login", {
          user_name: this.loginForm.username,
          user_pwd: this.loginForm.password,
          isadmin: 1,
        });
        console.log(res);
        if (res.code !== 1) return this.$message.error(res.msg);
        this.$message.success("登录成功！");
        // 1.将登录成功后的token，保存到客户端的sessionStorage中
        window.sessionStorage.setItem("username", res.data.user_name);
        window.sessionStorage.setItem("token", res.data.authorization);
        // 2.通过编程式导航跳转到后台主页，路由地址是 /home
        this.$router.replace("/home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.bgimg {
  position: absolute;
  top: 50%;
  left: 300px;
  margin-top: -214px;
}
.login_container {
  background-color: #71b1f1;
  height: 100%;
  .login_box {
    position: absolute;
    width: 450px;
    height: 600px;
    background-color: #fff;
    left: 80%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0px 0px 13px 5px #eee;
    .avatar_box {
      height: 200px;
      width: 100%;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
  .code {
    width: 200px;
  }
  .vcodeImg {
    float: right;
  }
  h2 {
    letter-spacing: 2px;
    margin-top: 20px;
    margin-bottom: 30px;
    display: block;
    text-align: center;
    color: #0092e7;
    font-size: 30px;
  }
}
.login_form {
  position: absolute;
  bottom: 10px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
